// JavaScript Document
if(!window.saf){
	window.saf = {};
}
//用户查询事件
saf.clicksearch = function(url){
	$(".key_body > .search_ac > .bt_search").click(function(e){
		var parameters = "?";
		$(".key_body > .input_list input,.key_body > .input_list select").each(function(){
			var name = $(this).attr("name"),value = $(this).attr("value");		
			if($.trim(name) != "" && $.trim(value) != ""){
				parameters = parameters + $(this).attr("name") + "=" + $(this).attr("value") + "&";
			}
		});
		parameters = parameters.substr(0,parameters.length-1);
		//alert(url + parameters);
		var shade = saf.shade = saf.loaddialog("table.table_1");
		saf.ajaxObj = $.getJSON(url + parameters, function(json){							 
			$(saf.shade[1]).find("p").html("请求成功！");
			saf.shade = null;
			$(shade[1]).append("<input type='button' value='关闭' />").find("input").click(function(){
				$(shade[1]).remove();
				$(shade[0]).remove();
				$("#RightBox select").show();
			});
		});
	});  	    
};
//
saf.loaddialog = function(selector){
	var element = $(selector),eposition,ezindex,shade = [];
	if(element.length == 0){
		element = $(document);
	}else if(element.length > 1){
		element = element[0]; 
	}
	eposition = element.position();
	ezindex = element.css("z-index");
	$("#RightBox select").hide();
	shade[0] = $("<div>").css({"width":element.width(),"height":element.height(),"left":eposition ? eposition.left : 0,"top":eposition ? eposition.top :0,"z-index":isNaN(ezindex) ? 999 : Number(ezindex)+1,"background": "#CCCCCC","opacity":.50,"position":"absolute"});
	$("#RightBox").append(shade[0])
	shade[1] = $("<div>").css({"width":200,"height":100,"z-index":shade[0].css("z-index")+1,"background":"#FFFFFF","position":"absolute","left":shade[0].position().left + shade[0].width()/2-100,"top":eposition.top+30,"border":"1px #d3dbe7 solid","text-align":"center"});
	$(shade[1]).append("<p>加载中...</p>");
	$("#RightBox").append(shade[1]);
	return shade;
};

saf.dialog = function(selector){
	var element = $(document),shade=[];
	$("#RightBox select").hide();
	shade[0] = $("<div>")
	.css({width:element.width(),height:element.height(),left: 0,top:0,"z-index":9999,background : "#CCCCCC",opacity:.50,position:"absolute"});
	$("body").append(shade[0]);
	shade[1] = $(selector).css({position:"absolute",left:$(document).scrollLeft() + (element.width()-$(selector).width())/2,top:$(document).scrollTop() + (element.height()-$(selector).height())/2,border:"1px #d3dbe7 solid","z-index":Number(shade[0].css("z-index"))+1,background : "#FFFFFF"}).show();
	
	return shade;
};
saf.getObjWh = function(obj){
	var st=$("body").scrollTop();//滚动条距顶部的距离
	var sl=$("body").scrollLeft();//滚动条距左边的距离
	var ch=$("body").height();//屏幕的高度
	var cw=$("body").width();//屏幕的宽度
	var objH=obj.height();//浮动对象的高度
	var objW=obj.width();//浮动对象的宽度
	var objT=Number(st)+(Number(ch)-Number(objH))/2;
	var objL=Number(sl)+(Number(cw)-Number(objW))/2;
	return objT+"|"+objL;
};

$(function(){
	
	saf.clicksearch("../../work/json.test.htm");
	
	
	
	
	
	$(".header").ajaxError(function(event,request, settings,thrownError){
		if(saf.ajaxObj = request)
		{
			$(saf.shade[1]).find("p").html("页面出错: " + request.status );
			$(saf.shade[1]).append("<input type='button' value='关闭' />").find("input").click(function(){
				$(saf.shade[0]).remove();
				$(saf.shade[1]).remove();
				$("#RightBox select").show();
				saf.shade = null;
			});
		}
	});
	$(".key_body > .search_ac > .bt_add,#RightBox table td > a.a_modify").click(function(){
		saf.shade = saf.dialog("#add");
		return false;
	});
	$("#add input.bt_save,#add input.bt_del_2,#set input.bt_save,#set input.bt_del_2,#add > h4 > .b_close > a,#set > h4 > .b_close > a").click(function(){
			$(saf.shade[0]).remove();
			$(saf.shade[1]).hide();
			$("#RightBox select").show();
			saf.shade = null;
			return false;
	});
	$("#RightBox table td > a.a_power").click(function(){
		saf.shade = saf.dialog("#set");
		return false;
	});
	
	$("#RightBox table td > a.a_flag_n,#RightBox table td > a.a_flag_y").click(function(){
		var shade = saf.loaddialog("table.table_1"),e = this,text,title,rclass,aclass,doc;
		if($(this).hasClass("a_flag_n")){
			doc="您准备启用";text = "已启用";title="点击关闭启用";rclass="a_flag_n";aclass="a_flag_y";
		}else{
			doc="您准备停用";text = "未启用";title="点击开启启用";rclass="a_flag_y";aclass="a_flag_n";
		}
		$(shade[1]).find("p").html(doc+ $(this).parent().siblings("td:eq(2)").text() +"用户？");
		$(shade[1]).append("<input type='button' value='确定' /><input type='button' value='消息' />").find("input").click(function(){
			if($(this).attr("value") === "确定"){
				$(e).text(text).attr("title",title).removeClass(rclass).addClass(aclass);
			}
			$(shade[1]).remove();
			$(shade[0]).remove();
			$("#RightBox select").show();
		});
	});

	$(window).scroll(function(){
		if(saf.shade){
			saf.shade[0].css({width:$(document).width(),height:$(document).height()});
			saf.shade[1].css({top:saf.getObjWh(saf.shade[1]).split("|")[0] + "px",left:saf.getObjWh(saf.shade[1]).split("|")[1] + "px"});
		}
	});
	$(window).resize(function(){
		if(saf.shade){
			saf.shade[0].css({width:$(document).width(),height:$(document).height()});
			saf.shade[1].css({top:saf.getObjWh(saf.shade[1]).split("|")[0] + "px",left:saf.getObjWh(saf.shade[1]).split("|")[1] + "px"});
		}
	});
	
	$("input[name=checkbox]").click(function(){
		if($("input[name=checkbox]").is(":not(:checked)")){
			$("input[name=all]").attr("checked",false);
		}else{
			$("input[name=all]").attr("checked",true);
		}
	});
	
	
	
	$("input[name=all]").click(function(){
		if($(this).is("[checked=true]")){
			$("input[name=checkbox]").attr("checked",true);
		}else{
			$("input[name=checkbox]").attr("checked",false)
		}
	});
	$("table a[title='删除所选数据']").click(function(){
		var usernames = "",shade = saf.loaddialog("table.table_1");
		$("input[name=checkbox]:checked").each(function(){
			usernames +=" " + $(this).parent().siblings("td:eq(1)").text() + ",";
			
		}); 
		usernames = usernames.substr(0,usernames.length-1);
		$(shade[1]).find("p").html("您确定要删除以下用户："+ usernames );
		$(shade[1]).append("<input type='button' value='确定' /><input type='button' value='消息' />").find("input").click(function(){
			if($(this).attr("value") === "确定"){
				$("input[name=checkbox]:checked").each(function(){
					$(this).parent().parent().remove();
				});
			}
			$(shade[1]).remove();
			$(shade[0]).remove();
			$("#RightBox select").show();
		});		
	});
});